const initPrizeList = () => {
    const list = [] as any
    for (let index = 0; index < 40; index++) {
        const item = {
            name: "奖品" + (index + 1),
            value: index + 1,
            index,
            img: "/src/assets/img/test.png",
        }
        list.push(item)
    }
    return list
}

const initSetting = async () => {
    const setting = {
        diceNumber: 7,
        useDice: false,
        prizeList: [],
        prizeNameHistory: [],
        prizePlan: [], // 奖品方案
        theme: '默认主题',
    } as any
    let userSetting = localStorage.getItem('userSetting') as any
    if (userSetting) {
        userSetting = JSON.parse(userSetting)
        setting.diceNumber = userSetting.diceNumber
        setting.useDice = userSetting.useDice
        setting.prizeNameHistory = userSetting.prizeNameHistory
        setting.prizePlan = userSetting.prizePlan
        setting.theme = userSetting.theme
        const hasDefaultPlan = setting.prizePlan.findIndex((item: any) => item.name === '默认方案')
        if (hasDefaultPlan === -1) { // 填充默认方案
            setting.prizePlan.unshift({
                name: '默认方案',
                data: initPrizeList()
            })
        }
        setting.prizeList = await checkPrizeImage(userSetting.prizeList)
        // 排序一下 checkPrizeImage之后顺序乱了
        setting.prizeList.sort((a: any, b: any) => a.index - b.index)
    }
    else { // 初始化数据
        setting.prizeList = initPrizeList()
        setting.prizePlan = [{
            name: '默认方案',
            data: setting.prizeList
        }]
        localStorage.setItem('userSetting', JSON.stringify(setting))
    }
    console.log('initSetting setting', setting)
    return setting
}

const resetPrizeList = async () => {
    const setting = await initSetting()
    setting.prizeList = initPrizeList()
    localStorage.setItem('userSetting', JSON.stringify(setting))
    return setting
}

const checkPrizeImage = (prizeList: any) => {
    // 检测图片的链接是否可用 图片是否存在
    let resList = [] as any
    return new Promise((resolve) => {
        prizeList.forEach((item: any) => {
            const img = new Image()
            img.src = item.img
            img.onload = () => {
                resList.push(item)
                if (resList.length === prizeList.length) resolve(resList)
            }
            img.onerror = () => {
                item.img = "/src/assets/img/test.png"
                resList.push(item)
                if (resList.length === prizeList.length) resolve(resList)
            }
        })
    })
}

const setDiceNumber = async (number: number) => {
    const setting = await initSetting()
    setting.diceNumber = number
    localStorage.setItem('userSetting', JSON.stringify(setting))
}

const setUseDice = async (useDice: boolean) => {
    const setting = await initSetting()
    setting.useDice = useDice
    localStorage.setItem('userSetting', JSON.stringify(setting))
}

const setPrizeList = async (prizeList: any) => {
    // 深拷贝
    const list = JSON.parse(JSON.stringify(prizeList))
    const setting = await initSetting()
    setting.prizeList = list
    localStorage.setItem('userSetting', JSON.stringify(setting))
}

const addPrizeNameHistory = async (keyword: string) => {
    const setting = await initSetting()
    const hasIndex = setting.prizeNameHistory.findIndex((item: string) => item === keyword)
    if (hasIndex === -1) {
        setting.prizeNameHistory.push(keyword)
        localStorage.setItem('userSetting', JSON.stringify(setting))
    }
}

const addPlan = async (name: string, prizeList: any) => {
    return new Promise(async (resolve) => {
        const setting = await initSetting()
        if (!setting.prizePlan) setting.prizePlan = []
        setting.prizePlan.push({
            name,
            data: prizeList
        })
        localStorage.setItem('userSetting', JSON.stringify(setting))
        resolve(setting.prizePlan)
    })
}

const deleteOnePlan = async (index: number) => {
    const setting = await initSetting()
    setting.prizePlan.splice(index, 1)
    localStorage.setItem('userSetting', JSON.stringify(setting))
    return setting.prizePlan
}

const deleteAllPlan = async () => {
    const setting = await initSetting()
    setting.prizePlan = []
    localStorage.setItem('userSetting', JSON.stringify(setting))
}

const setTheme = async (theme: string) => {
    const setting = await initSetting()
    setting.theme = theme
    localStorage.setItem('userSetting', JSON.stringify(setting))
}

const blockListMode1 = () => {
    return [
        {
            value: 1,
            left: "4%",
            top: "81%",
        },
        {
            value: 2,
            left: "4%",
            top: "69%",
        },
        {
            value: 3,
            left: "4%",
            top: "56%",
        },
        {
            value: 4,
            left: "4%",
            top: "43%",
        },
        {
            value: 5,
            left: "4%",
            top: "30%",
        },
        {
            value: 6,
            left: "4%",
            top: "17.5%",
        },
        {
            value: 7,
            left: "4%",
            top: "5%",
        },
        {
            value: 8,
            left: "14.5%",
            top: "5%",
        },
        {
            value: 9,
            left: "25%",
            top: "5%",
        },
        {
            value: 10,
            left: "36%",
            top: "5%",
        },
        {
            value: 11,
            left: "46.5%",
            top: "5%",
        },
        {
            value: 12,
            left: "57%",
            top: "5%",
        },
        {
            value: 13,
            left: "68%",
            top: "5%",
        },
        {
            value: 14,
            left: "78.5%",
            top: "5%",
        },
        {
            value: 15,
            left: "89%",
            top: "5%",
        },
        {
            value: 16,
            left: "89%",
            top: "17.5%",
        },
        {
            value: 17,
            left: "89%",
            top: "30%",
        },
        {
            value: 18,
            left: "78.5%",
            top: "30%",
        },
        {
            value: 19,
            left: "68%",
            top: "30%",
        },
        {
            value: 20,
            left: "57%",
            top: "30%",
        },
        {
            value: 21,
            left: "46.5%",
            top: "30%",
        },
        {
            value: 22,
            left: "36%",
            top: "30%",
        },
        {
            value: 23,
            left: "25%",
            top: "30%",
        },
        {
            value: 24,
            left: "25%",
            top: "43%",
        },
        {
            value: 25,
            left: "25%",
            top: "56%",
        },
        {
            value: 26,
            left: "36%",
            top: "56%",
        },
        {
            value: 27,
            left: "46.5%",
            top: "56%",
        },
        {
            value: 28,
            left: "57%",
            top: "56%",
        },
        {
            value: 29,
            left: "68%",
            top: "56%",
        },
        {
            value: 30,
            left: "78.5%",
            top: "56%",
        },
        {
            value: 31,
            left: "89%",
            top: "56%",
        },
        {
            value: 32,
            left: "89%",
            top: "69%",
        },
        {
            value: 33,
            left: "89%",
            top: "81%",
        },
        {
            value: 34,
            left: "78.5%",
            top: "81%",
        },
        {
            value: 35,
            left: "68%",
            top: "81%",
        },
        {
            value: 36,
            left: "57%",
            top: "81%",
        },
        {
            value: 37,
            left: "46.5%",
            top: "81%",
        },
        {
            value: 38,
            left: "36%",
            top: "81%",
        },
        {
            value: 39,
            left: "25%",
            top: "81%",
        },
        {
            value: 40,
            left: "14.5%",
            top: "81%",
        },
    ]
}

const blockListMode2 = () => {
    return [
        {
            value: 1,
            left: "6%",
            top: "65%",
        },
        {
            value: 2,
            left: "6%",
            top: "50%",
        },
        {
            value: 3,
            left: "6%",
            top: "34%",
        },
        {
            value: 4,
            left: "6%",
            top: "19%",
        },
        {
            value: 5,
            left: "6%",
            top: "4%",
        },
        {
            value: 6,
            left: "29%",
            top: "4%",
        },
        {
            value: 7,
            left: "51.5%",
            top: "4%",
        },
        {
            value: 8,
            left: "75%",
            top: "4%",
        },
        {
            value: 9,
            left: "75%",
            top: "19%",
        },
        {
            value: 10,
            left: "75%",
            top: "34%",
        },
        {
            value: 11,
            left: "75%",
            top: "50%",
        },
        {
            value: 12,
            left: "75%",
            top: "65%",
        },
        {
            value: 13,
            left: "75%",
            top: "81%",
        },
        {
            value: 14,
            left: "51.5%",
            top: "81%",
        },
        {
            value: 15,
            left: "29%",
            top: "81%",
        },
    ]
}


export {
    initSetting,
    setDiceNumber,
    setUseDice,
    setPrizeList,
    addPrizeNameHistory,
    resetPrizeList,
    addPlan,
    deleteOnePlan,
    deleteAllPlan,
    setTheme,
    blockListMode1,
    blockListMode2,
}